/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_FACTORY_HPP
#define	_CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_FACTORY_HPP

#include <cubefs/api/types.hpp>

#include <cubefs/core/tools/types/typelist.hpp>
#include <cubefs/core/tools/types/relation.hpp>


#include <cubefs/core/btree/pages/dispatchers/defs.hpp>
#include <cubefs/core/btree/pages/dispatchers/tools.hpp>

namespace cubefs    {
namespace btree     {


using namespace cubefs::tools;
using cubefs::TypeList;

template <
    typename LevelList,
    typename NodeTypeList,
    typename RootNode,
    typename LeafNode,
    typename RootLeafNode,
    typename Node,
    typename NodeBase,
    typename PageMgr,
    typename Txn
    >
class NodeFactoryHelper;

template <
    typename NodeTypeList,
    typename RootNode,
    typename LeafNode,
    typename RootLeafNode,
    typename Node,
    typename NodeBase,
    typename PageMgr,
    typename Txn
    >
class NodeFactoryHelper<NullType, NodeTypeList, RootNode, LeafNode, RootLeafNode, Node, NodeBase, PageMgr, Txn> {
public:
    static NodeBase *Create(PageMgr &mgr, Int level, bool root, bool leaf) {
        NodeBase* node;
        if (!root && !leaf) {
            //node = new (mgr) Node;
        	node = (Node*) mgr.create_new();
        	node->init();
            node->page_type_hash() = Node::hash();
        }
        else if (!root && leaf) {
            //node = new (mgr) LeafNode;
        	node = (LeafNode*) mgr.create_new();
        	node->init();
            node->page_type_hash() = LeafNode::hash();
        }
        else if (root && !leaf) {
            //node = new (mgr) RootNode;
        	node = (RootNode*) mgr.create_new();
        	node->init();
            node->page_type_hash() = RootNode::hash();
        }
        else {
            //node = new (mgr) RootLeafNode();
        	node = (RootLeafNode*) mgr.create_new();
        	node->init();
            node->page_type_hash() = RootLeafNode::hash();
        }

        node->level() = level;
        node->set_root(root);
        node->set_leaf(leaf);
        return node;
    }
};


template <
    typename Head,
    typename Tail,
    typename NodeTypeList,
    typename DefRootNode,
    typename DefLeafNode,
    typename DefRootLeafNode,
    typename DefNode,
    typename NodeBase,
    typename PageMgr,
    typename Txn
    >
class NodeFactoryHelper<TypeList<Head, Tail>, NodeTypeList, DefRootNode, DefLeafNode, DefRootLeafNode, DefNode, NodeBase, PageMgr, Txn> {

    static const Int Level = Head::Value;

    typedef typename NodeFilter<ValueOp<LEVEL, EQ, Short, Level>, NodeTypeList>::Result LevelList;

    struct BaseWrapper {
        typedef NodeBase Type;
    };

    template <bool Root, bool Leaf>
    struct ListBuilder {
        typedef typename SelectHeadIfNotEmpty <
                typename NodeFilter <
                    And <
                        ValueOp<ROOT, EQ, bool, Root>,
                        ValueOp<LEAF, EQ, bool, Leaf>
                    >,
                    LevelList
                >::Result,
                BaseWrapper
        >::Result                                                               Type;
    };

    typedef typename ListBuilder<false, false>::Type                            Node;
    typedef typename ListBuilder<false, true>::Type                             LeafNode;
    typedef typename ListBuilder<true, false>::Type                             RootNode;
    typedef typename ListBuilder<true, true>::Type                              RootLeafNode;

public:
    static NodeBase *Create(PageMgr &mgr, Int level, bool root, bool leaf) {
        //std::cout<<"HERE C2 "<<Level<<" "<<level<<std::endl;
        if (Level == level)
        {
            NodeBase* node;

            if (!root && !leaf)
            {
                //node = new (mgr) Node;
            	node = (Node*) mgr.create_new();
				node->init();
                node->page_type_hash() = Node::hash();
            }
            else if (!root && leaf)
            {
                //node = new (mgr) LeafNode;
            	node = (LeafNode*) mgr.create_new();
            	node->init();
                node->page_type_hash() = LeafNode::hash();
            }
            else if (root && !leaf)
            {
                //node = new (mgr) RootNode;
            	node = (RootNode*) mgr.create_new();
            	node->init();
                node->page_type_hash() = RootNode::hash();
            }
            else
            {
                //node = new (mgr) RootLeafNode;
            	node = (RootLeafNode*) mgr.create_new();
            	node->init();
                node->page_type_hash() = RootLeafNode::hash();
            }

            node->level() = level;
            node->set_root(root);
            node->set_leaf(leaf);

            return node;
        }
        else {
            return NodeFactoryHelper<
                        Tail,
                        NodeTypeList,
                        DefRootNode,
                        DefLeafNode,
                        DefRootLeafNode,
                        DefNode,
                        NodeBase,
                        PageMgr,
                        Txn
                   >::Create(mgr, level, root, leaf);
        }
    }
};


template <
    typename NodeTypeList,
    typename NodeBase,
    typename PageMgr,
    typename Txn
>
class NodeFactoryTool: public NodeFactoryHelper <
                                typename LevelListBuilder<NodeTypeList>::List,
                                NodeTypeList,

                                typename FindNodeWithMaxLevelTool<true,  false, NodeTypeList>::Type,
                                typename FindNodeWithMaxLevelTool<false, true,  NodeTypeList>::Type,
                                typename FindNodeWithMaxLevelTool<true,  true,  NodeTypeList>::Type,
                                typename FindNodeWithMaxLevelTool<false, false, NodeTypeList>::Type,

                                NodeBase,
                                PageMgr,
                                Txn
                          >
{};




}
}

#endif //_CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_FACTORY_HPP
